﻿using System.Collections.Generic;
using System.Linq;
using ARMIR.util;
using RimWorld;
using Verse;

namespace ARMIR {
    /// <summary>
    /// 能力-战术装备02，在目标地区造成大范围伤害并在中心伤害较高
    /// </summary>
    public class CompAbilityEffect_TMZeroTwo : CompAbilityEffect {
        public new CompProperties_TMZeroTwo Props => (CompProperties_TMZeroTwo)props;
        // 设置延迟
        TickUtil DelayTick = Current.Game.GetComponent<TickUtil>();
        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest) {
            //一段小范围高伤害
            base.Apply(target, dest);
            EffecterDef TMLandBefore = DefDatabase<EffecterDef>.GetNamed("ARMIR_TMZeroTwo_EffecterB"); //技能特效(落地前预警)
            TMLandBefore.Spawn(target.Cell, parent.pawn.Map);
            var targetPosition = target.Cell;
            if (DelayTick != null) {
                DelayTick.TickTimeManager(30, () => {
                    speDraw.DragLight(parent.pawn.Map, parent.pawn.Position.ToVector3(), 10, 0.15f, 15, false, true, 0,0.3f);
                }, true);
                DelayTick.TickTimeManager(280,() => {
                    speDraw.DragLight(parent.pawn.Map, targetPosition.ToVector3(), 30, 0.15f, 30, true, false, 120,0.8f);
                },true);
                DelayTick.TickTimeManager(300, () => {

                    List<Thing> ExplosiveIgnoredPawns = new List<Thing>();
                    ExplosiveIgnoredPawns = parent.pawn.Map.mapPawns.AllPawnsSpawned.Where(p => p.Faction == Faction.OfPlayer && p.Position.DistanceTo(target.Cell) < Props.radius + 2).OfType<Thing>().ToList();
                    ExplosiveIgnoredPawns.AddRange(parent.pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.HaulableEver)).Where(p => p.Position.DistanceTo(target.Cell) < Props.radius + 2));
                    ExplosiveIgnoredPawns.AddRange(parent.pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.Corpse)).Where(p => p.Position.DistanceTo(target.Cell) < Props.radius + 2));
                    ExplosiveIgnoredPawns.Add(parent.pawn);

                    float randomDamageDef = UnityEngine.Random.value;
                    DamageDef DamageDef = DamageDefOf.Cut;
                    if (randomDamageDef > Props.CutToStab) DamageDef = DamageDefOf.Stab;
                    //随机技能音效
                    SoundDef randomSound = null;
                    if (UnityEngine.Random.value > 0.5) {
                        randomSound = DefDatabase<SoundDef>.GetNamed("ARMIR_secondHitA");
                    } else {
                        randomSound = DefDatabase<SoundDef>.GetNamed("ARMIR_secondHitB");
                    }
                    EffecterDef TMLand = DefDatabase<EffecterDef>.GetNamed("ARMIR_TMZeroTwo_EffecterA"); //技能特效
                    TMLand.Spawn(targetPosition, parent.pawn.Map);
                    float powerBase = ARMIR_ModSettings.powerSet;
                    // 爆炸效果1
                    GenExplosion.DoExplosion(
                        targetPosition,
                        parent.pawn.Map,
                        radius: Props.radiusIn, //较小范围
                        DamageDef,
                        instigator: parent.pawn,
                        damAmount: (int)Props.damageAmount * 2 * (int)powerBase,
                        armorPenetration: (int)Props.armorPenetration * 3 * (int)powerBase,
                        damageFalloff: false, //伤害随距离衰减开启
                        screenShakeFactor: 1f, //屏幕震动强度(默认为1
                        explosionSound: randomSound, 
                        ignoredThings: ExplosiveIgnoredPawns
                    );
                    // 爆炸效果2
                    GenExplosion.DoExplosion(
                        targetPosition,
                        parent.pawn.Map,
                        radius: Props.radius,
                        DamageDefOf.Blunt,
                        instigator: parent.pawn,
                        damAmount: (int)Props.damageAmount * (int)powerBase,
                        armorPenetration: (int)Props.armorPenetration * (int)powerBase,
                        damageFalloff: false, //伤害随距离衰减开启
                        screenShakeFactor: 0.2f, //屏幕震动强度(默认为1
                        explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_fakeEmpty"),
                        ignoredThings: ExplosiveIgnoredPawns
                    );
                    // 爆炸效果重复（余震-隔一段时间生效
                    if (DelayTick != null)
                    {
                        DelayTick.TickTimeManager(40, () => {
                            GenExplosion.DoExplosion(
                                targetPosition,
                                parent.pawn.Map,
                                radius: Props.radius,
                                DamageDefOf.Blunt,
                                instigator: parent.pawn,
                                damAmount: (int)Props.damageAmount / 2 * (int)powerBase, //余震伤害减半
                                armorPenetration: (int)Props.armorPenetration / 2 * (int)powerBase,
                                damageFalloff: true, //伤害随距离衰减开启
                                screenShakeFactor: 0.4f, //屏幕震动强度(默认为1
                                explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_fakeEmpty"),
                                ignoredThings: ExplosiveIgnoredPawns
                            );
                        }, true);
                    }
                }, true);
            }
            //负面效果
            bool hasEquipment = parent.pawn != null &&
            (parent.pawn.equipment.AllEquipmentListForReading.Any(e =>
            e.def == DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer")) ||
            parent.pawn.apparel.WornApparel.Any(a => a.def == DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer")));
            if (hasEquipment)
            {
                parent.pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase<ThoughtDef>.GetNamed("ARMIR_MindLoad_weak")); //技能施放的思维负担
            }
            else
            {
                parent.pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase<ThoughtDef>.GetNamed("ARMIR_MindLoad")); //技能施放的思维负担
            }

        }

        public override void DrawEffectPreview(LocalTargetInfo target) {
            GenDraw.DrawRadiusRing(target.Cell, Props.radius);
            GenDraw.DrawRadiusRing(target.Cell, Props.radiusIn);
        }
    }
}